package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomDouble;
import br.com.telescope.adsrt.dom.DomId;
import br.com.telescope.adsrt.dom.DomInteger;
import br.com.telescope.adsrt.dom.DomText;
import br.com.telescope.adsrt.dom.DomUpper;
import br.com.telescope.adsrt.lib.AdsException;
import br.com.telescope.adsrt.lib.Field;
import br.com.telescope.adsrt.lib.Form;
import br.com.telescope.sql.ModelQuery;
import br.com.telescope.t2.EntityBinding;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.feature.DeleteFeature;
import br.com.telescope.t2.feature.ListFeature;
import br.com.telescope.t2.feature.LookupFeature;
import br.com.telescope.t2.feature.MultiRowInsertFeature;
import br.com.telescope.t2.feature.MultiRowUpdateFeature;
import br.com.telescope.t2.feature.ViewFeature;
import br.com.telescope.util.ObjectUtil;
import java.util.ArrayList;
import java.util.List;
import solutions.ferpa.ztest.ent.*;

/**
 * Modelos para ensaios em instrumento base.
 * <p>
 * Interface para manutenção de Modelo para ensaio no Instrumento.
 * <P>
 * Telescope origin: ZTEST.CAD_MODELO_ENSAIO_INSTRUMENTO
 * <P>
 * Base entity: ZTEST.MODELO_ENSAIO_INTRUMENTO
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class CadModeloEnsaioInstrumento extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "ZTEST.CAD_MODELO_ENSAIO_INSTRUMENTO";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Modelos para ensaios em instrumento base");
    }
    
    protected EntModeloEnsaioIntrumento base;
    protected EntModeloHidrometro mdlh;
    protected EntVazaoPrefixoSerie vpfs;
    protected EntModeloEnsaioIntrumento rec; // NEED_TJW1_COMPATIBILITY
    
    // Service parameters
    private DomId pIdInstrumentoEnsaio;
    
    // Record items
    private DomInteger aQuantidadesModeloEnsaio;
    private DomId aId;
    private DomId aInstrumentoEnsaioId;
    private DomId aModeloHidrometroId;
    private DomUpper aMdlhCodigo;
    private DomText aMdlhNome;
    private DomInteger aMdlhDiametroNominal;
    private DomDouble aVpfsVazaoNominal;
    
    // Query items
    
    @Override
    public String getBaseSql() {
        return "select BASE.*"
                + " , MDLH.CODIGO as MDLH_CODIGO"
                + " , MDLH.DESCRICAO as MDLH_NOME"
                + " , MDLH.DIAMETRO_NOMINAL as MDLH_DIAMETRO_NOMINAL"
                + " , VPFS.VAZAO_NOMINAL as VPFS_VAZAO_NOMINAL"
                + " , MDLH.ID as Z_MDLH_ID"
                + " , VPFS.ID as Z_VPFS_ID"
                + " from MODELO_ENSAIO_INTRUMENTO BASE"
                + " join MODELO_HIDROMETRO MDLH on MDLH.ID = BASE.MODELO_HIDROMETRO_ID"
                + " join VAZAO_PREFIXO_SERIE VPFS on VPFS.ID = MDLH.VAZAO_PREFIXO_SERIE_ID";
    }
    
    @Override
    public void initAttributes() {
        
        aQuantidadesModeloEnsaio = new DomInteger();
        aQuantidadesModeloEnsaio.context(ctx).name("QUANTIDADES_MODELO_ENSAIO").label("Quantidades deste modelo (por ensaio)");
        
        aId = new DomId();
        aId.context(ctx).name("ID").label("Id");
        
        aInstrumentoEnsaioId = new DomId();
        aInstrumentoEnsaioId.context(ctx).name("INSTRUMENTO_ENSAIO_ID").label("Instrumento de ensaio");
        
        aModeloHidrometroId = new DomId();
        aModeloHidrometroId.context(ctx).name("MODELO_HIDROMETRO_ID").label("Modelo de hidrômetro");
        
        aMdlhCodigo = new DomUpper();
        aMdlhCodigo.context(ctx).name("MDLH_CODIGO").label("Código do modelo de hidrômetro");
        aMdlhCodigo.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT CODIGO as valor"
                + " , CODIGO as pesquisar"
                + " , CODIGO as descricao"
                + " from MODELO_HIDROMETRO MDLH)", "q"));
        aMdlhCodigo.setAutocompleteLength(2);
        
        aMdlhNome = new DomText();
        aMdlhNome.context(ctx).name("MDLH_NOME").label("Descrição do hidrômetro");
        aMdlhNome.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT DESCRICAO as valor"
                + " , DESCRICAO as pesquisar"
                + " , DESCRICAO as descricao"
                + " from MODELO_HIDROMETRO MDLH)", "q"));
        aMdlhNome.setAutocompleteLength(2);
        
        aMdlhDiametroNominal = new DomInteger();
        aMdlhDiametroNominal.context(ctx).name("MDLH_DIAMETRO_NOMINAL").label("Diâmetro nominal (mm)");
        aMdlhDiametroNominal.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT DIAMETRO_NOMINAL as valor"
                + " , DIAMETRO_NOMINAL as pesquisar"
                + " , DIAMETRO_NOMINAL as descricao"
                + " from MODELO_HIDROMETRO MDLH)", "q"));
        aMdlhDiametroNominal.setAutocompleteLength(2);
        
        aVpfsVazaoNominal = new DomDouble();
        aVpfsVazaoNominal.context(ctx).name("VPFS_VAZAO_NOMINAL").label("Vazão nominal (m3/h)");
        aVpfsVazaoNominal.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT VAZAO_NOMINAL as valor"
                + " , VAZAO_NOMINAL as pesquisar"
                + " , VAZAO_NOMINAL as descricao"
                + " from VAZAO_PREFIXO_SERIE VPFS)", "q"));
        aVpfsVazaoNominal.setAutocompleteLength(0);
        
    }
    
    @Override
    public void initParameters() {
        pIdInstrumentoEnsaio = new DomId(ctx, "ID_INSTRUMENTO_ENSAIO", "Instrumento para ensaio");
        pIdInstrumentoEnsaio.setValue(ctx.getParameter("P_ID_INSTRUMENTO_ENSAIO"));
        pIdInstrumentoEnsaio.setSqlExpression("INSTRUMENTO_ENSAIO_ID");
        if (pIdInstrumentoEnsaio.isNotNull()) {
            aInstrumentoEnsaioId.setValue(pIdInstrumentoEnsaio.getValue());
        }
    }
    
    @Override
    public List<Field> getParameters() {
        if (allParameters == null) {
            allParameters = new ArrayList<Field>();
            allParameters.add(pIdInstrumentoEnsaio);
        }
        return allParameters;
    }
    
    @Override
    public void copyBindParameters() {
        EntModeloEnsaioIntrumento record = getRecord();
        if (pIdInstrumentoEnsaio.isNotNull()) {
            record.setInstrumentoEnsaioId(pIdInstrumentoEnsaio.getValue());
        }
    }
    
    @Override
    public void retrieveBindParameters() {
        pIdInstrumentoEnsaio.setValue(getRecord().valueOfInstrumentoEnsaioId());
    }
    
    @Override
    public void initEntityBindings() {
        EntityBinding binding;
        
        binding = new EntityBinding("ZTEST.MODELO_HIDROMETRO", "MDLH", "Z_MDLH_ID", "ZTEST.CAD_MODELO_HIDROMETRO.VIEW");
        binding.addFields(aMdlhCodigo, aMdlhNome, aMdlhDiametroNominal);
        entityBindings.add(binding);
    }
    
    @Override
    public String getQueryPkColumn() {
        return "BASE.ID";
    }
    
    @Override
    public void setId(String value) {
        this.aId.setValue(value);
    }
    
    @Override
    public EntModeloEnsaioIntrumento getRecord() {
        if (base == null) {
            base = new EntModeloEnsaioIntrumento(ctx);
            rec = base;
        }
        return this.base;
    }
    
    @Override
    public String getContextName() {
        return aMdlhCodigo.toText();
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "MDLH_CODIGO, MDLH_NOME, VPFS_VAZAO_NOMINAL, MDLH_DIAMETRO_NOMINAL";
    }
    
    @Override
    public void translateInsertFks(Form form) {
        ModelQuery query;
        
        // MODELO_HIDROMETRO_ID
        try {
            if (form.hasFields("MDLH_CODIGO")) {
                query = createQuery()
                        .select("MDLH.ID")
                        .from("MODELO_HIDROMETRO", "MDLH")
                        .filter("MDLH.CODIGO", form.get("MDLH_CODIGO"))
                        ;
                base.setModeloHidrometroId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    @Override
    public void translateUpdateFks(Form form) {
        ModelQuery query;
        
        // MODELO_HIDROMETRO_ID
        try {
            if (form.hasFields("MDLH_CODIGO")) {
                query = createQuery()
                        .select("MDLH.ID")
                        .from("MODELO_HIDROMETRO", "MDLH")
                        .filter("MDLH.CODIGO", form.get("MDLH_CODIGO"))
                        ;
                base.setModeloHidrometroId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LIST">
    public class FList extends ListFeature {
        
        @Override
        public void initForm() {
            addField(aMdlhCodigo, "view");
            addField(aMdlhNome, "view");
            addField(aVpfsVazaoNominal, "view");
            addField(aMdlhDiametroNominal, "view");
            addField(aQuantidadesModeloEnsaio, "view");
        }
        
        {
            setFeatureName("LIST");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="VIEW">
    public class FView extends ViewFeature {
        
        @Override
        public void initForm() {
            addField(aMdlhCodigo, "view");
            addField(aMdlhNome, "view");
            addField(aVpfsVazaoNominal, "view");
            addField(aMdlhDiametroNominal, "view");
            addField(aQuantidadesModeloEnsaio, "view");
        }
        
        {
            setFeatureName("VIEW");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT">
    public class FInsert extends MultiRowInsertFeature {
        
        @Override
        public void initForm() {
            aMdlhCodigo.setOptional(false);
            addField(aMdlhCodigo, "rqd");
            aMdlhNome.setEditable(false);
            addField(aMdlhNome, "view");
            aVpfsVazaoNominal.setEditable(false);
            addField(aVpfsVazaoNominal, "view");
            aMdlhDiametroNominal.setEditable(false);
            addField(aMdlhDiametroNominal, "view");
            aQuantidadesModeloEnsaio.setOptional(false);
            addField(aQuantidadesModeloEnsaio, "rqd");
        }
        
        {
            setFeatureName("INSERT");
            setPrimary(false);
            setRows(10);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="UPDATE">
    public class FUpdate extends MultiRowUpdateFeature {
        
        @Override
        public void initForm() {
            aMdlhCodigo.setOptional(false);
            addField(aMdlhCodigo, "rqd");
            aMdlhNome.setEditable(false);
            addField(aMdlhNome, "view");
            aVpfsVazaoNominal.setEditable(false);
            addField(aVpfsVazaoNominal, "view");
            aMdlhDiametroNominal.setEditable(false);
            addField(aMdlhDiametroNominal, "view");
            aQuantidadesModeloEnsaio.setOptional(false);
            addField(aQuantidadesModeloEnsaio, "rqd");
        }
        
        {
            setFeatureName("UPDATE");
            setPrimary(false);
            setRows(10);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="DELETE">
    public class FDelete extends DeleteFeature {
        
        @Override
        public void initForm() {
            addField(aMdlhCodigo, "view");
            addField(aMdlhNome, "view");
            addField(aVpfsVazaoNominal, "view");
            addField(aMdlhDiametroNominal, "view");
        }
        
        {
            setFeatureName("DELETE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="MDLH_CODIGO_LOOKUP">
    public class FMdlhCodigoLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aMdlhCodigo.setEditable(true);
            addField(aMdlhCodigo, "opt");
            aMdlhNome.setEditable(false);
            addField(aMdlhNome, "view");
            aMdlhDiametroNominal.setEditable(false);
            addField(aMdlhDiametroNominal, "view");
            aVpfsVazaoNominal.setEditable(false);
            addField(aVpfsVazaoNominal, "view");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("MDLH.CODIGO as MDLH_CODIGO")
                    .select("MDLH.DESCRICAO as MDLH_NOME")
                    .select("MDLH.DIAMETRO_NOMINAL as MDLH_DIAMETRO_NOMINAL")
                    .select("VPFS.VAZAO_NOMINAL as VPFS_VAZAO_NOMINAL")
                    .from("MODELO_HIDROMETRO", "MDLH")
                    .leftJoin("VAZAO_PREFIXO_SERIE", "VPFS", "VPFS.ID = MDLH.VAZAO_PREFIXO_SERIE_ID")
                    ;
        }
        
        {
            setFeatureName("MDLH_CODIGO_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FList());
        addFeature(new FView());
        addFeature(new FInsert());
        addFeature(new FUpdate());
        addFeature(new FDelete());
        addFeature(new FMdlhCodigoLookup());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "LIST";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public Integer getQuantidadesModeloEnsaio() {
        return aQuantidadesModeloEnsaio.getValue();
    }
    
    public void setQuantidadesModeloEnsaio(Integer value) {
        this.aQuantidadesModeloEnsaio.setValue(value);
    }
    
    @Override
    public Long getId() {
        return aId.getValue();
    }
    
    public void setId(Long value) {
        this.aId.setValue(value);
    }
    
    public Long getInstrumentoEnsaioId() {
        return aInstrumentoEnsaioId.getValue();
    }
    
    public void setInstrumentoEnsaioId(Long value) {
        this.aInstrumentoEnsaioId.setValue(value);
    }
    
    public Long getModeloHidrometroId() {
        return aModeloHidrometroId.getValue();
    }
    
    public void setModeloHidrometroId(Long value) {
        this.aModeloHidrometroId.setValue(value);
    }
    
    public String getMdlhCodigo() {
        return aMdlhCodigo.getValue();
    }
    
    public void setMdlhCodigo(String value) {
        this.aMdlhCodigo.setValue(value);
    }
    
    public String getMdlhNome() {
        return aMdlhNome.getValue();
    }
    
    public void setMdlhNome(String value) {
        this.aMdlhNome.setValue(value);
    }
    
    public Integer getMdlhDiametroNominal() {
        return aMdlhDiametroNominal.getValue();
    }
    
    public void setMdlhDiametroNominal(Integer value) {
        this.aMdlhDiametroNominal.setValue(value);
    }
    
    public Double getVpfsVazaoNominal() {
        return aVpfsVazaoNominal.getValue();
    }
    
    public void setVpfsVazaoNominal(Double value) {
        this.aVpfsVazaoNominal.setValue(value);
    }
    
    public Long getParameterIdInstrumentoEnsaio() {
        return pIdInstrumentoEnsaio.getValue();
    }
    
    public void setParameterIdInstrumentoEnsaio(Long value) {
        pIdInstrumentoEnsaio.setValue(value);
        aInstrumentoEnsaioId.setValue(value);
    }
    // </editor-fold>
    
    
    @Override
    public String getJsServiceFile() {
        return "ui/ztest/cad_modelo_ensaio_instrumento.js";
    }
}
